<?php


namespace App\Helpers;


use App\Models\ActionLog;
use App\Models\Bill;
use App\Models\BillLog;
use App\Models\OnJobLog;
use App\Models\PerformanceTable;
use App\Models\Post;
use App\Models\SystemUser;


trait PerFormanceTrait
{
    use Untils;

    //获取工作审核通过后开始时间
    private function getTime($post_id){
        $time =  OnJobLog::query()->where($post_id)
            ->where('status',1)
            ->value('checked_at');
        if($time){
            return strtotime(date('Y-m-d',$time));
        }
        return $time;
    }
    //获质保期
    private function getExpire($post_id){
       return Post::query()
            ->where('type',2)
            ->where('id',$post_id)->value('expire');
    }
    //上岗后第几天开始产生费用
    private function getJobDays($post_id){
        $expire = $this->getExpire($post_id);
        if($expire==1) {
            return 16;
        }else{
          return 7;
        }
    }
    //获取该岗位上月开始算钱时间
    private function getJobStart($post_id){
        $last_time = $this->firstOfMonth(2);
        $create_at= $this->getTime($post_id);
        if ($create_at){
            $days = $this->getJobDays($post_id);
                if($days>0){//非销售型
                    //业绩开始换算时间
                    $start = $create_at+$days*3600*24;
                    if($start<$last_time){//上个月已经开始算钱
                        return $last_time;
                    }else{
                        return $start;
                    }
                }
        }else{
            return 0;
        }
    }
    //上个月该岗位产生的费用
    private function getLastMonthFee($post_id){
        $first = $this->getJobStart($post_id);
        $end = $this->endOfMonth();
        return $this->sumFee($first,$end,$post_id);
    }
    //总上岗天数
    private function getOnJobAllDays($start,$post_id){
        $days = $this->getJobDays($post_id);//16 或者 0
        $diffDays = ceil(($this->endOfMonth(2)-strtotime($start))/86400);
        if($days==16){
            if($diffDays<16){
                return 0;
            }else{
                return $diffDays;
            }
        }
        if($days==7){
            if($diffDays<8){
                return 0;
            }else{
                return $diffDays;
            }
        }
        return 0;
    }
    //月上岗开始计费天数
    private function getLastJobDays($start,$post_id){
        $days = $this->getJobDays($post_id);//16 或者 0
        $end = $this->endOfMonth(2);
        $nowDiffDay = ceil((time()-strtotime($start))/86400);
        $diffDay = ceil(($end-strtotime($start))/86400);
        if($days==7){
            if($nowDiffDay<8){//小于16天则不计费
                return 0;
            }
            if($diffDay>30){
                return 30;
            }
            return $diffDay;
        }
        if($days==16){
            if($nowDiffDay<16){//小于16天则不计费
                return 0;
            }
            if($diffDay>90){
                return 90;
            }
        }
        //开始时间获取月初时间
        return $diffDay;
    }
    //上月岗位总充值余额
    public function getAllBills($post_id){
        $first = $this->firstOfMonth();
        $end = $this->endOfMonth() ;
        return Bill::query()
            ->whereBetween('updated_at',[$first,$end])
            ->where('status',2)
            ->where('post_id',$post_id)
            ->sum('received');
    }


    //总充值>总消耗 则获取上个业绩=上个月的工作量
    //总充值<总消耗 则获取上个月总充值金额
    //  如果上个月总消耗<上个月总充值   则 获取上个月总消耗
    // 如果上个月总消耗>大于上个月总充值 则 获取总充值
    private function getTotal($recharge,$all_days,$v,$last_performance){
        info('总充值'.$recharge);
        info('历史业绩'.$recharge);
        $type = $this->this_day($v->post_id);
        if($type==90){
            $all_price = $v->actual_price*$all_days/90;
        }else{
            $all_price = $v->actual_price*$all_days/30;
        }
        info('总消耗:'.$all_price);
        info('总天数'.$all_days);
        //加上保险费和路费
        $add_total_amount = $all_price+$v->toll+$v->insurance;
        if($recharge>=$add_total_amount){

            info('上个月价格'.$last_performance);
            if($last_performance&&$last_performance>0){
                return $all_price-$last_performance;
            }
            return $all_price;
        }else{
            return 0;
        }
    }
    //获取上个月总充值
    private function getLastMonthAllRecharge($id){
        //上个月总充值
        $bill_log = BillLog::query()
            ->where('bills_id',$id)
            ->whereBetween('updated_at',[$this->beginLastMonth(),$this->endLastMonth()])
            ->where('status',2)
            ->sum('received');
        if($bill_log){
            return $bill_log;
        }else{
            return 0;
        }
    }
    //根据时间换算消耗
    private function sumFee($start,$end,$post_id){
        $days =ceil(($end-$start)/86400);
        $actual_price = OnJobLog::query()->where('post_id',$post_id)->value('actual_price');
        if($actual_price){
            return $actual_price*$days;
        }else{
            return 0;
        }
    }
    //获取上月总上岗数组
    private function getJob(){
        $arr = OnJobLog::query()
            ->whereBetween('checked_at',[$this->firstOfMonth(2),$this->endOfMonth(2)])
            ->get();
        return $arr;
    }
    //获取提成比例
    private function getUserRate($user_id){
        return  SystemUser::query()->where('id',$user_id)->value('rate');
    }
    //上岗天数
    private function getDays($start,$closed = null){
        if($closed){
            $times = strtotime($closed)-strtotime($start);
        }else{
            $times = time()-strtotime($start);
        }
        return floor($times/24/3600);
    }
    private function getFee($value,$days,$price,$this_day){
        if($value==1){
            $day = $days-16;
            if($day>=0&&$this_day>0){
                return ceil($price*$days/$this_day);
            }
        }else{
            $day = $days-7;
            if($day>0&&$this_day>0){
                return ceil($price*$days/$this_day);
            }
            return 0;
        }
    }
    private function getUniPrice($post_id,$price){
        $expire = Post::query()->where('id',$post_id)->value('expire');
        if($expire==1){
            return $price/90;
        }else{
            return $price/30;
        }
    }

    //获取退款时总实际入账金额
    public function getRefundFactFee($v){
        return BillLog::query()
            ->where('status',2)
            ->where('bills_id',$v->id)
            ->sum('received');
    }
    private function this_day($post_id){
        $expire = $this->getExpire($post_id);
        if($expire==1){
            $this_day = 90;
        }else{
            $this_day = 30;
        }
        return $this_day;
    }
}
